package com.bell.atensettings.settingDetails;

import java.util.ArrayList;
import java.util.List;

import com.bell.atensettings.R;
import com.bell.atensettings.bean.DisplayItems;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class InputMethodSettings extends Activity implements OnItemClickListener
{
    private int mIndex = 0;
    private ArrayList<DisplayItems> mInputInfoList;
    private ListView mInputList = null;
    private TextView mTitleView;
    private ImageView bridgeImage;

    private void initItem()
    {
        mInputInfoList = new ArrayList<DisplayItems>();
        InputMethodManager mimm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        List<InputMethodInfo> mImis = mimm.getInputMethodList();
        String curInput = Settings.Secure.getString(getContentResolver(), "default_input_method");

        for (int i = 0; i < mImis.size(); i++)
        {
            DisplayItems inputItem = new DisplayItems();
            PackageManager pm = getPackageManager();
            
            inputItem.setData(mImis.get(i).getId());
            inputItem.setName(mImis.get(i).loadLabel(pm).toString());
            
            if (inputItem.getData().equals(curInput))
                mIndex = i;
            
            mInputInfoList.add(inputItem);
        }
    }
    
    private void setDefaultInputMethod(String inputMethod)
    {
        StringBuffer enableInput = new StringBuffer(Settings.Secure.getString(getContentResolver(), "enabled_input_methods"));
        
        if (!enableInput.toString().contains(inputMethod))
        {
            enableInput.append(":").append(inputMethod);
            
            Settings.Secure.putString(getContentResolver(), "enabled_input_methods", enableInput.toString());
        }
        
        Settings.Secure.putString(getContentResolver(), "default_input_method", inputMethod);
    }
    
    private void initView()
    {
        ItemAdapter adapter = new ItemAdapter(this);
        mTitleView = (TextView) findViewById(R.id.setting_title);
        mInputList = (ListView) findViewById(R.id.listview);
        
        mTitleView.setText(getIntent().getStringExtra("title"));
        mInputList.setAdapter(adapter);
        mInputList.setOnItemClickListener(this);
    }
    
    @Override
    public void onBackPressed()
    {
        super.onBackPressed();
        overridePendingTransition(R.anim.start, R.anim.push_left_out);
    }
    
    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        setContentView(R.layout.public_settings);

        initItem();
        initView();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        setDefaultInputMethod(mInputInfoList.get(position).getData());
        finish();
    }

    public class ItemAdapter extends BaseAdapter
    {

        public ItemAdapter(Context context)
        {
            super();
        }

        public int getCount()
        {
            return mInputInfoList.size();
        }

        public Object getItem(int i)
        {
            return mInputInfoList.get(i);
        }

        public long getItemId(int i)
        {
            return (long) i;
        }

        public View getView(int position, View view, ViewGroup viewgroup)
        {
            DisplayItems items = mInputInfoList.get(position);
            if (view == null)
                view = ((LayoutInflater) getSystemService("layout_inflater")).inflate(
                        R.layout.list_display_item, null);
            view.setBackgroundResource(R.drawable.listnormal);
            ImageView imageview = (ImageView) view.findViewById(R.id.img_sign);
            TextView textview = (TextView) view.findViewById(R.id.tv_data);
            if (position == mIndex)
            {
                bridgeImage = imageview;
                imageview.setImageResource(R.drawable.right_icon);
            }
            else
            {
                imageview.setImageResource(R.drawable.unright_icon);
            }
            textview.setText(items.getName());
            view.setTag(imageview);
            return view;
        }
    }
}
